﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ConsumeSys.Handle
{
    /// <summary>
    /// 全局异常过滤，2019-9-7 14:34:38 王浩力 添加
    /// </summary>
    public class ExceptionHandleFilter : System.Web.Mvc.IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            filterContext.ExceptionHandled = true;
            var isAjax = filterContext.RequestContext.HttpContext.Request.IsAjaxRequest();
            string msg = filterContext.Exception.Message;
            if (filterContext.Exception.InnerException!=null)
            {
                msg = filterContext.Exception.InnerException.Message;
            }
            if (isAjax)
            {
                JsonResult jsonResult = new JsonResult();
                jsonResult.Data = new { state = "fail", msg = msg };
                filterContext.Result = jsonResult;
                return;
            }
            ContentResult contentResult = new ContentResult();
            contentResult.Content = filterContext.Exception.ToString(); 
            filterContext.Result = contentResult;
        }


    }
}